package cn.kgc.crowd1048.fundingadmin.service.impl;

import cn.kgc.crowd1048.fundingadmin.common.CrowdFundingUtils;
import cn.kgc.crowd1048.fundingadmin.pojo.Admin;
import cn.kgc.crowd1048.fundingadmin.mapper.AdminMapper;
import cn.kgc.crowd1048.fundingadmin.service.AdminService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zwr
 * @since 2024-04-17
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Resource
    AdminMapper adminMapper;
    @Resource
    BCryptPasswordEncoder bCryptPasswordEncoder;


    /**
     * 查询账户是否存在
     * @param loginAcct
     * @param userPswd
     * @return
     */
    @Override
    public Admin login(String loginAcct, String userPswd) {
        QueryWrapper<Admin> wrapper=new QueryWrapper<>();
        wrapper.eq("login_acct",loginAcct);
        List<Admin> list = adminMapper.selectList(wrapper);
        if (CrowdFundingUtils.collectionEffective(list)==false){
            return null;
        }
        Admin admin = list.get(0);
        if (admin==null){
            return null;
        }
        //加密输入的密码后与数据库中的加密密码 对照
        String md5 = CrowdFundingUtils.md5(userPswd);
        if (Objects.equals(md5,admin.getUserPswd())){
            return admin;
        }
        return null;
    }

    /**
     * 分页查询  条件查询
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @Override
    public PageInfo<Admin> queryForKeywordSearch(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Admin> pageInfo=null;
        QueryWrapper<Admin> wrapper=new QueryWrapper<>();
        if (keyword!=null&&keyword.length()>0){
            wrapper.like("login_acct",keyword)
                    .or()
                    .like("user_name",keyword)
                    .or()
                    .like("email",keyword);
        }
        List<Admin> list=adminMapper.selectList(wrapper);
        pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 批量删除
     * @param adminIdList
     */
    @Override
    public void batchRemove(List<Integer> adminIdList) {
        adminMapper.deleteBatchIds(adminIdList);
    }

    /**
     *  添加用户
     * @param admin
     */
    @Override
    public void saveAdmin(Admin admin) {
        //使用md5加密 密码
        String userPswd = admin.getUserPswd();
//        userPswd = CrowdFundingUtils.md5(userPswd);
        userPswd=bCryptPasswordEncoder.encode(userPswd);
        admin.setUserPswd(userPswd);
        // 执行保存
        adminMapper.insert(admin);
    }

    /**
     * 根据id查询用户
     * @param adminId
     * @return
     */
    @Override
    public Admin getAdminById(Integer adminId) {
        QueryWrapper<Admin> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",adminId);
        return adminMapper.selectOne(queryWrapper);
    }

    /**
     * 更新用户信息  密码加密后更新
     * @param admin
     */
    @Override
    public void updateAdmin(Admin admin) {
        // 对密码进行加密
        if (CrowdFundingUtils.stringEffective(admin.getUserPswd())){
            String userPswd = admin.getUserPswd();
//            userPswd = CrowdFundingUtils.md5(userPswd);
            userPswd=bCryptPasswordEncoder.encode(userPswd);
            admin.setUserPswd(userPswd);
        }
        adminMapper.updateById(admin);

    }


}
